package flex.cc.uac.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.base.enums.ErrorCodeEnum;
import flex.cc.core.annotation.LogAnnotation;
import flex.cc.core.annotation.ValidateAnnotation;
import flex.cc.core.support.BaseController;
import flex.cc.core.utils.RequestUtil;
import flex.cc.uac.dto.ModifyStatusDto;
import flex.cc.uac.entity.UacRoleEntity;
import flex.cc.uac.entity.UacRoleUserEntity;
import flex.cc.uac.enums.UacRoleStatusEnum;
import flex.cc.uac.exceptions.UacBizException;
import flex.cc.uac.service.UacRoleService;
import flex.cc.uac.service.UacRoleUserService;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/uac/role")
public class UacRoleMainController extends BaseController {

    @Resource
    private UacRoleService uacRoleService;
    @Resource
    private UacRoleUserService uacRoleUserService;
    /**
     * 分页查询角色信息.
     * @return
     */
    @PostMapping(value = "/queryRoleListWithPage")
    @ApiOperation(httpMethod = "POST", value = "查询角色列表")
    public Wrapper<IPage<UacRoleEntity>> queryUacRoleListWithPage(@ApiParam(name = "role", value = "角色信息") @RequestBody UacRoleEntity role) {
        logger.info("查询角色列表roleQuery={}", role);
        IPage<UacRoleEntity> page = uacRoleService.queryRoleListWithPage(role);
        return WrapMapper.ok(page);
    }

    /**
     * 删除角色信息.
     */
    @LogAnnotation
    @PostMapping(value = "/deleteRoleById/{id}")
    @ApiOperation(httpMethod = "POST", value = "删除角色")
    public Wrapper deleteUacRoleById(@ApiParam(name = "id", value = "角色id") @PathVariable Long id) {
        boolean result = uacRoleService.deleteRoleById(id);
        return super.handleResult(result);
    }

    /**
     * 批量删除角色.
     */
    @LogAnnotation
    @PostMapping(value = "/batchDeleteByIdList")
    @ApiOperation(httpMethod = "POST", value = "批量删除角色")
    public Wrapper batchDeleteByIdList(@ApiParam(name = "deleteIdList", value = "角色Id") @RequestBody List<Long> deleteIdList) {
        logger.info("批量删除角色 idList={}", deleteIdList);
        uacRoleService.batchDeleteByIdList(deleteIdList);
        return WrapMapper.ok();
    }

    /**
     * 修改角色状态.
     */
    @LogAnnotation
    @PostMapping(value = "/modifyRoleStatusById")
    @ApiOperation(httpMethod = "POST", value = "根据角色Id修改角色状态")
    public Wrapper modifyUacRoleStatusById(@ApiParam(name = "modifyRoleStatusDto", value = "修改角色状态数据")
                                               @RequestBody ModifyStatusDto modifyStatusDto) {
        logger.info("根据角色Id修改角色状态 modifyStatusDto={}", modifyStatusDto);
        Long roleId = modifyStatusDto.getId();
        if (roleId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }

        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Long userId = loginAuthDto.getUserId();

        UacRoleUserEntity ru = uacRoleUserService.getByUserIdAndRoleId(userId, roleId);

        if (ru != null && UacRoleStatusEnum.DISABLE.getType().equals(modifyStatusDto.getStatus())) {
            throw new UacBizException(ErrorCodeEnum.UAC10012002);
        }

        UacRoleEntity uacRole = new UacRoleEntity();
        uacRole.setId(roleId);
        uacRole.setStatus(modifyStatusDto.getStatus());
        uacRole.setUpdateInfo(loginAuthDto);

        boolean result = uacRoleService.updateById(uacRole);
        return super.handleResult(result);
    }


    /**
     * 保存角色
     */
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增角色")
    @ValidateAnnotation
    @LogAnnotation
    public Wrapper save(@ApiParam(name = "role", value = "角色信息") @RequestBody UacRoleEntity role) {
        LoginAuthDto loginAuthDto = RequestUtil.getLoginUser();
        uacRoleService.saveRole(role, loginAuthDto);
        return WrapMapper.ok();
    }
}
